﻿using YiShop.System.Constants;
using YiShop.System.Repositories;

namespace YiShop.System.Services
{
    public class SysRoleService : BaseService<SysRole, SysRoleDto>
    {
        private readonly SysRoleRepository _sysRoleRepository;
        private readonly SysRoleMenuRepository _sysRoleMenuRepository;
        public SysRoleService(SysRoleRepository sysRoleRepository)
        {
            BaseRepo = _sysRoleRepository = sysRoleRepository;
        }

        public async Task<List<string>> GetRolePermissionByUserId(long userId)
        {
            List<SysRoleDto> perms = await GetDtoListAsync(new SysRoleDto { UserId = userId });
            List<string> permsSet = new List<string>();
            foreach (SysRoleDto perm in perms)
            {
                if (perm != null)
                {
                    permsSet.AddRange(perm.RoleKey!.Trim().Split(","));
                }
            }
            return permsSet;
        }

        /// <summary>
        /// 查询 角色信息表 详情
        /// </summary>
        public async Task<SysRoleDto> GetDtoAsync(long id)
        {
            var entity = await FirstOrDefaultAsync(e => e.RoleId == id);
            var dto = entity.Adapt<SysRoleDto>();
            // TODO 填充关联表数据
            return dto;
        }

        internal async Task<bool> CheckRoleNameUniqueAsync(SysRoleDto dto)
        {
            long roleId = dto.RoleId;
            SysRole info = await _sysRoleRepository.GetFirstAsync(new SysRoleDto { RoleName = dto.RoleName });
            if (info != null && info.RoleId != roleId)
            {
                return UserConstants.NOT_UNIQUE;
            }
            return UserConstants.UNIQUE;
        }

        internal async Task<bool> CheckRoleKeyUniqueAsync(SysRoleDto dto)
        {
            long roleId = dto.RoleId;
            SysRole info = await _sysRoleRepository.GetFirstAsync(new SysRoleDto { RoleKey = dto.RoleKey });
            if (info != null && info.RoleId != roleId)
            {
                return UserConstants.NOT_UNIQUE;
            }
            return UserConstants.UNIQUE;
        }

        /// <summary>
        /// 新增保存角色信息
        /// </summary>
        public virtual async Task<int> InsertRoleAsync(SysRoleDto role)
        {
            // 新增角色信息
            role.DelFlag = DelFlag.No;
            await InsertAsync(role);
            return await InsertRoleMenuAsync(role);
        }

        /// <summary>
        /// 新增角色菜单信息
        /// </summary>
        public async Task<int> InsertRoleMenuAsync(SysRoleDto role)
        {
            int rows = 1;
            // 新增用户与角色管理
            List<SysRoleMenu> list = new List<SysRoleMenu>();
            foreach (long menuId in role.MenuIds)
            {
                SysRoleMenu rm = new SysRoleMenu
                {
                    RoleId = role.RoleId,
                    MenuId = menuId
                };
                list.Add(rm);
            }
            if (list.Count > 0)
            {
                rows = await _sysRoleMenuRepository.InsertAsync(list);
            }
            return rows;
        }

        internal Task<int> UpdateRoleStatusAsync(SysRoleDto role)
        {
            return UpdateAsync(role);
        }

        public async Task<List<SysRoleDto>> GetRolesByUserIdAsync(long userId)
        {
            var dto = new SysRoleDto { DelFlag = DelFlag.No };
            var roles = await GetDtoListAsync(dto);

            foreach (var role in roles)
            {
                if (role.UserId.Equals(userId))
                {
                    role.Flag = true;
                }
            }

            return roles;
        }
    }
}
